<!DOCTYPE html>
<html>
<head>
    <title>鹿鹿泽健身工作室教练工作台</title>
    <link rel="stylesheet" href="../static/css/minghu.css">
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="background-color:#efec81">
<div class="login-container">
        <div class="login-box">
            <div id="logo">
                <img src="{{ url_for('static', filename='pics/logo_llz.png') }}" alt="llzlogo" style="width:90px;height:auto;opacity: 0.86;">
                </div>
            <div style="margin:15px;">教练登录</div>
            <div id="userBox" style="font-size: 10px;">手机<input id="user" style="margin:5px;width:100px;" oninput="checkMobileValid()"></div>
            <div id="mobileValidResult" style="font-size:8px;color:rgb(201, 91, 91)"> </div>
            
            <div id="pwdBox" style="font-size: 10px;">密码<input id="pwd" type="password" style="margin:5px;width:100px;"></div>
            <div id="loginInfo"></div>
            <hr>
            <button id="login" onclick="login()">登录</button>
        </div>
</div>
    
    <script src="../static/js/common.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded',function(){
            // if(localStorage.getItem('isLogin')==='true'){
            //     window.location.href='/main';
            // }
            localStorage.removeItem('isLogin');
        });

        function login(){
            const user=document.getElementById('user').value;
            const pwd=document.getElementById('pwd').value;

            const isValidMobile=checkMobileValid();
            if(isValidMobile==='ok' && isValidMobile!=='' && pwd!==''){
                fetch('/deal_login',{
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                    },
                body:JSON.stringify({'user':user,'pwd':pwd})                
                })
                .then(response=>response.json())
                .then(data=>{
                    const loginInfo=document.getElementById('loginInfo');
                    if(data.res==='ok'){
                        if(data.reedit_pwd==='yes'){
                            alert('需要修改密码')
                            window.location.href = '/reedit_pwd';
                        }else{
                            // localStorage.setItem('isLogin','true');
                            console.log(data.url)
                            loginInfo.textContent='';
                            window.location.href = data.url;
                        }
                    }else{
                        loginInfo.textContent='用户名或密码错误';
                    }
                }
                )
                .catch(error=>{
                    console.log(error);
                });       
            }else{
                loginInfo.textContent='用户名或密码错误';
            }          
                
            
        }

        function checkMobileValid(){
            const mobilePattern = /^1[3-9]\d{9}$/;
            const mobile=document.getElementById('user').value;
            const mobileValidResult=document.getElementById('mobileValidResult');
            mobileValidResult.textContent='';
            if(mobile && mobile!==''){
                if(!mobilePattern.test(mobile)){
                    mobileValidResult.textContent='无效的手机号码'
                    return 'not ok'
                }
            }
            return 'ok'
        }

        

    </script>
</body>